package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/rest/httpx"
	"go-exp/pkg/errorx"
	"go-exp/pkg/httpresult"
	"go-exp/zero/api/internal/config"
	"go-exp/zero/api/internal/handler"
	"go-exp/zero/api/internal/svc"
	"net/http"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "zero/api/etc/api.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	callback := rest.WithUnauthorizedCallback(func(w http.ResponseWriter, r *http.Request, err error) {
		// 自定义处理返回
		httpresult.HttpResult(r, w, nil, errorx.NewCodeError(401, "登录信息已过期,请重新登录"))
	})
	server := rest.MustNewServer(c.RestConf, callback)
	defer server.Stop()

	httpx.SetErrorHandler(errorx.HttpErrorHandler)

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
